<template>
    <div class="loginBox">
        <div class="loginMain">
            <div class="loginItem">
                <div class="itemLabel">手机号/邮箱</div>
                <input type="text" v-model="username" class="itemInput" placeholder="请输入手机号或邮箱"/>
            </div>
            <div class="loginItem">
                <span class="itemLabel">密码</span>
                <input type="password" v-model="password" class="itemInput" placeholder="请输入密码"/>
            </div>
            <div class="loginItem loginForgetPassRegister">
                <span @click="jumpToRegister">去注册</span>
                <span>忘记密码</span>
            </div>
            <div class="loginBtn" @click="loginBtn">登录</div>
        </div>
    </div>
</template>

<script>
import { login } from '@/api/user';
import {instance}  from '@/socketio';
export default {
    name: 'LoginView',
    data() {
        return {
            username: '',
            password: '',
        };
    },
    created() {
        console.log('login_created');
        if(this.$store.getters.token && instance.socket && instance.socket.connected){
            this.$router.push('/w');
        }
    },
    methods: {
        // 跳转到注册
        jumpToRegister() {
            this.$router.push('/register');
        },
        // 登录按钮
        loginBtn() {
            if(!this.username || !this.password){
                alert('请输入用户名或密码');
                return;
            }
            login({account: this.username, password: this.password}).then(res => {
                console.log('login res', res);
                if(res && res.code === 20000){
                    const token = res.token;
                    const userInfo = res.data;
                    console.log('login success', token);
                    this.$store.dispatch('setToken', token); // 保存token
                    instance.connect(token); // 连接socketio
                    // 监听登录成功
                    instance.once('login', (obj) => {
                        console.log('once_login', obj);
                        if(obj.code === 20000 && obj.uniqueId === token){
                            // 登录成功
                            this.$store.dispatch('setUserInfo', userInfo); // 保存用户信息
                            // 跳转到聊天页面
                            this.$router.replace('/w');
                        }else {
                            alert(obj.message || '登录失败');
                        }
                    });
                    instance.once('errorLogin', (obj) => {
                        console.log('login_errorLogin', obj);
                    });
                } else {
                    alert(res.message || '登录失败');
                }
            }).catch(err => {
                console.error('login err', err);
                alert(err);
            });
        }
    },
}
</script>

<style scoped>
.loginBox{
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;
}
.loginMain{
    width: 400px;
    padding: 20px;
    box-shadow: 0 0 10px rgba(0,0,0,.1);
    border-radius: 8px;
    background: var(--loginMainBackground);
}
.loginItem{
    margin-bottom: 20px;
}
.itemLabel{
    font-size: 14px;
}
.itemInput{
    width: 100%;
    height: 50px;
    line-height: 40px;
    padding: 0 10px;
    box-sizing: border-box;
    border: 1px solid #ddd;
    border-radius: 4px;
    outline: none;
    font-size: 14px;
    margin-top: 10px;
}
.loginBtn{
    width: 100%;
    height: 50px;
    line-height: 50px;
    text-align: center;
    background: var(--loginBtnBackground);
    color: var(--loginBtnColor);
    border-radius: 4px;
    cursor: pointer;
    user-select: none;
}
.loginForgetPassRegister{
    display: flex;
    justify-content: space-between;
    font-size: 14px;
}
</style>